package com.itcam.interview.pro00.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class AwaitSignalExample {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        Thread threadA = new Thread(() -> {
            lock.lock();
            try {
                // 模拟任务执行
                System.out.println("线程A执行任务...");
                // 暂停线程A，等待通知
                condition.await();
                System.out.println("线程A继续执行任务...");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lock.unlock();
            }
        });

        Thread threadB = new Thread(() -> {
            lock.lock();
            try {
                // 模拟任务执行
                System.out.println("线程B来唤醒线程...");
                // 通知线程A继续执行
                condition.signal();
            } finally {
                lock.unlock();
            }
        });

        threadA.start();
        threadB.start();
    }
}
